package com.hzeng.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateTimeManagerUtil {
	private static DateTimeManagerUtil util = new DateTimeManagerUtil();
	
	public static DateTimeManagerUtil getInstance() {
		return util;
	}
	
	public Date getDateNow() {
		return Calendar.getInstance().getTime();
	}
	
	public Calendar getCalendarNow() {
		return Calendar.getInstance();
	}
	
	//convert and parse and format with each other(java.util.Calendar and java.util.Date and java.sql.Date) 
	
	public Date convert(Calendar calendar) {
		return calendar.getTime();
	}
	
	public Calendar convert(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}
	
	public Date convertToDate(java.sql.Date date) {
		return new Date(date.getTime());
	}
	
	public java.sql.Date convertToSQLDate(Date date) {
		return new java.sql.Date(date.getTime());
	}
	
	public String format(Date date,String format) {
		if(date == null || format == null) {
			return null;
		}
		SimpleDateFormat sdf = new  SimpleDateFormat(format,Locale.ENGLISH);
		return sdf.format(date);
	}
	
	public String format(Calendar calendar,String format) {
		if(calendar == null || format == null) {
			return null;
		}
		return format(convert(calendar), format);
	}
	
	public String format(java.sql.Date date,String format) {
		return format(convertToDate(date), format);
	}
	
	public java.sql.Date parseToSQLDate(String dateString,String format) {
		return new java.sql.Date(parseToDate(dateString, format).getTime());	
	}
	
	public Date parseToDate(String dateString,String format) {
		if(dateString == null || format == null) {
			return null;
		}
		SimpleDateFormat sdf = new  SimpleDateFormat(format,Locale.ENGLISH);
		try {
			return sdf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public Calendar parseToCalendar(String dateString,String format) {
		if(dateString == null || format == null) {
			return null;
		}
		SimpleDateFormat sdf = new  SimpleDateFormat(format,Locale.ENGLISH);
		try {
			return convert(sdf.parse(dateString));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String newFormatString(String dateString,String oldFormat,String newFormat) {
		return format(parseToDate(dateString, oldFormat), newFormat);
	}
	
	public Date setDate(Date date,int year,int month,int day,int hour,int minute,int second,int millsecond) {
		return setCalendar(convert(date), year, month, day, hour, minute, second, millsecond).getTime();
	}
	
	public java.sql.Date setSQLDate(java.sql.Date date,int year,int month,int day,int hour,int minute,int second,int millsecond) {
		return convertToSQLDate(setDate(convertToDate(date), year, month, day, hour, minute, second, millsecond));
	}
	
	public Calendar setCalendar(Calendar calendar,int year,int month,int day,int hour,int minute,int second,int millsecond) {
		if(calendar == null) {
			return null;
		}
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DATE, day);
		calendar.set(Calendar.HOUR, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND, millsecond);
		calendar.getTime();
		return calendar;
	}
	
	public Calendar setCalendar(Calendar calendar,int field,int value) {
		if(calendar == null) {
			return null;
		}
		calendar.set(field, value);
		calendar.getTime();
		return calendar;
	}
	
	public Date setDate(Date date ,int field,int value) {
		if(date == null) {
			return null;
		}
		
		return setCalendar(convert(date), field, value).getTime();
	}
	
	public java.sql.Date setSQLDate(java.sql.Date date ,int field,int value) {
		return convertToSQLDate(setDate(convertToDate(date), field, value));
	}
	
	public int getCalendarOfField(Calendar calendar,int field) {
		//for example:field = Calendar.Year
		return calendar.get(field);
	}
	
	public int getDateOfField(Date date, int field) {
		//for example:field = Calendar.Year
		return getCalendarOfField(convert(date), field);
	}
	
	public int getSQLDateOfField(java.sql.Date date ,int field) {
		//for example:field = Calendar.Year
		return getCalendarOfField(convert(date), field);
	}
	

	//compare time with each other(Date and java.sql.Date and Calendar)
	
	public boolean beforeDate(Date date1, Date date2) {
		if(date1 == null || date2 == null) {
			System.out.println("date1 or date2 is null,can not be compared");
			return false;
		}
		return date1.before(date2);
	}
	
	public boolean beforeCalendar(Calendar calendar1, Calendar calendar2) {
		if(calendar1 == null || calendar2 == null) {
			System.out.println("calendar1 or calendar2 is null,can not be compared");
			return false;
		}
		return calendar1.before(calendar2);
	}
	
	public boolean beforeSQLDate(java.sql.Date date1, java.sql.Date date2) {
		if(date1 == null || date2 == null) {
			System.out.println("date1 or date2 is null,can not be compared");
			return false;
		}
		return date1.before(date2);
	}
	
	public Date millsToDate(long mills) {
		Date d = new Date(mills);
		return d;
	}
	
	public Date timeFormatToDate(String time,String format) {
		if(time == null || format == null) {
			return null;
		}
		
		
		/*TimeZone sysTimezone = TimeZone.getDefault();
		System.out.println(sysTimezone);
		
		String tempTimezone = "GMT+00:00";
		TimeZone.setDefault(TimeZone.getTimeZone(tempTimezone));

		
		Calendar today = Calendar.getInstance(TimeZone.getTimeZone(tempTimezone));*/
		Calendar today = Calendar.getInstance();
		today = this.setCalendar(today, Calendar.HOUR_OF_DAY, 0);
		today = this.setCalendar(today, Calendar.MINUTE, 0);
		today = this.setCalendar(today, Calendar.SECOND, 0);
		today = this.setCalendar(today, Calendar.MILLISECOND, 0);
		
		Calendar temp = this.parseToCalendar(time, format);
		System.out.println(temp);
		
		System.out.println(temp.getTimeInMillis());
		System.out.println(19 * 3600 * 1000);
		
		long to = today.getTimeInMillis();
		long te = temp.getTimeInMillis();
		long newDatemills = to + te;
		System.out.println(newDatemills);
		
		System.out.println(parseToDate("2011-10-12 19:00:00 0", "yyyy-MM-dd HH:mm:ss S").getTime());
		
		return new Date(newDatemills);
	}
	
	
	
	public static void main(String[] args) {
		/*Date date = new Date();
		Date testDate = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss S");
		try {
			testDate = sdf.parse("2011-09-17 12:44:00 000");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if(date.before(testDate)) {
			System.out.println(date.toString() + " is early than " + testDate.toString());
		}else {
			System.out.println(date.toString() + " is late than " + testDate.toString());
		}*/
		DateTimeManagerUtil util = DateTimeManagerUtil.getInstance();
		System.out.println(util.timeFormatToDate("19:00:00 GMT", "HH:mm:ss z"));
		System.out.println(new Date());
		

	}

}
